using System;
using System.Runtime.Serialization;
using EnvDTE;
using Microsoft.Practices.RecipeFramework;

namespace PrismFactory.SoftwareFactory.References
{
    [Serializable]
    public class ModuleProjectReference : UnboundRecipeReference
    {
        private const string IsModuleProjectVariable = "IsModuleProject";

        public ModuleProjectReference(string recipe) : base(recipe)
        {
        }

        public ModuleProjectReference(SerializationInfo info, StreamingContext context) : base(info, context)
        {
        }

        public override string AppliesTo
        {
            get { return "Any module project."; }
        }

        public override bool IsEnabledFor(object target)
        {
            if (target is Project)
            {
                var project = (Project) target;
                return IsModuleProject(project);
            }
            if (target is ProjectItem)
            {
                var item = (ProjectItem) target;
                return IsModuleProject(item.ContainingProject);
            }
            return false;
        }

        private bool IsModuleProject(Project project)
        {
            if (project.Globals.get_VariableExists(IsModuleProjectVariable))
            {
                if (project.Globals[IsModuleProjectVariable].ToString() == Boolean.TrueString)
                {
                    return true;
                }
            }
            return false;
        }
    }
}